package Client;

import SQL.Connections;

import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

//修改密码
public class ChangePassword {
    private JPanel panel1;
    private JButton 确认Button;
    private JPasswordField oldPasswordField;
    private JPasswordField newPasswordField1;
    private JPasswordField newPasswordField2;
    private JLabel infoLabel;//提示标签
    private JLabel title;
    private JButton 返回Button;
    public static JFrame f;
    public static String user;

    private Connection con;
    private Statement stm;
    private ResultSet rs;
    private Connections connections = new Connections();


    public ChangePassword() {
        确认Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {



                String username = user;
                String pw = oldPasswordField.getText();
                String newpw1 = newPasswordField1.getText();
                String newpw2 = newPasswordField2.getText();

                changePassword(username,pw,newpw1,newpw2);
            }
        });

        //按下enter代替点击鼠标
        newPasswordField2.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
                if (e.getKeyChar() == '\n'){

                    String username = user;
                    String pw = oldPasswordField.getText();
                    String newpw1 = newPasswordField1.getText();
                    String newpw2 = newPasswordField2.getText();

                    changePassword(username,pw,newpw1,newpw2);
                }
            }
        });
        返回Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                PassengerHome.initialize(user);
                f.dispose();
            }
        });
    }

    private void changePassword(String username,String pw,String newpw,String newpw2) {
        try{
            Connection con = connections.getConnection();
            stm = con.createStatement();
            //用手机号
            rs = stm.executeQuery("SELECT * FROM usertrain WHERE UserPhoneNumber='"+username+"'");

            while (rs.next()){
                //数据库里面的密码
                String realpw = rs.getString("UserPassword").toString().trim();

                if(pw.equals(realpw) && newpw.equals(newpw2)){
                    if(newpw.length()>=6 && newpw.length()<=15){

                        stm.executeUpdate("UPDATE usertrain SET UserPassword='"+newpw+"' WHERE UserPhoneNumber='"+username+"'");



                        JOptionPane.showMessageDialog(panel1,"修改密码成功");
                    }else if(newpw.length()<6){
                        JOptionPane.showMessageDialog(panel1,"密码长度不够");
                    }else{
                        JOptionPane.showMessageDialog(panel1,"密码长度不能超过15位");
                    }

                }else if(pw.equals(realpw)){
                    JOptionPane.showMessageDialog(panel1,"两次输入的新密码不相同");
                }else{
                    JOptionPane.showMessageDialog(panel1,"原密码错误");
                }





            }


        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        initialize();


    }

    public static void initialize() {
        initialize(user);

    }

    public static void initialize(String use){
        JFrame frame = new JFrame("ChangePassword");
        frame.setContentPane(new ChangePassword().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        f=frame;
        frame.setVisible(true);
        user=use;
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
        title = new JLabel(new ImageIcon("key.png"));
    }
}
